using System;
using System.Web.Mvc;
using RMH.DataAccess;
using RMH.Entities;

namespace RMH.Web.Models.Binders
{
    public class DiagnosisBinder : IModelBinder
    {
        private IDiagnosisRepository diagonosisRepository;

        public DiagnosisBinder(IDiagnosisRepository diagonosisRepository)
        {
            this.diagonosisRepository = diagonosisRepository;
        }

        public DiagnosisBinder()
        {
            diagonosisRepository = Container.Resolve<DiagnosisRepository>();
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var form = controllerContext.HttpContext.Request.Form;

            Diagnosis rv = new Diagnosis();

            if (!String.IsNullOrEmpty(form["Id"]))
                rv = diagonosisRepository.GetByID(Int32.Parse(form["Id"]));

            rv.Name = form["Name"];

            return rv;
        }
    }
}